﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

using TinyLib.Input;
using TinyLib.Sprite;
using TinyLib.Entity;

namespace GodBuilderIdea
{
    public class PlayerMenu : Entity
    {
        EnvTile.EnvironmentType m_tile_selected = EnvTile.EnvironmentType.GRASS;
        List<EnvTile> m_tile_choices;
        Sprite tile_glow;

        public PlayerMenu(Game game, SpriteBatch sprite_batch)
            : base(game, sprite_batch)
        {
            Initialize();

            m_tile_choices = new List<EnvTile>();
            EnvTile newTile = new EnvTile(Game, Spritebatch);
            newTile.m_type = EnvTile.EnvironmentType.DESERT;
            newTile.position = new Vector2(1500, 200);
            newTile.m_changed_type = true;
            m_tile_choices.Add(newTile);

            newTile = new EnvTile(Game, Spritebatch);
            newTile.m_type = EnvTile.EnvironmentType.FOREST;
            newTile.position = new Vector2(1500, 265);
            newTile.m_changed_type = true;
            m_tile_choices.Add(newTile);

            newTile = new EnvTile(Game, Spritebatch);
            newTile.m_type = EnvTile.EnvironmentType.GRASS;
            newTile.position = new Vector2(1500, 330);
            newTile.m_changed_type = true;
            m_tile_choices.Add(newTile);

            newTile = new EnvTile(Game, Spritebatch);
            newTile.m_type = EnvTile.EnvironmentType.WATER;
            newTile.position = new Vector2(1468, 395);
            newTile.m_changed_type = true;
            m_tile_choices.Add(newTile);

            newTile = new EnvTile(Game, Spritebatch);
            newTile.m_type = EnvTile.EnvironmentType.SNOW;
            newTile.position = new Vector2(1435, 200);
            newTile.m_changed_type = true;
            m_tile_choices.Add(newTile);

            newTile = new EnvTile(Game, Spritebatch);
            newTile.m_type = EnvTile.EnvironmentType.MOUNTAIN;
            newTile.position = new Vector2(1435, 265);
            newTile.m_changed_type = true;
            m_tile_choices.Add(newTile);

            newTile = new EnvTile(Game, Spritebatch);
            newTile.m_type = EnvTile.EnvironmentType.SWAMP;
            newTile.position = new Vector2(1435, 330);
            newTile.m_changed_type = true;
            m_tile_choices.Add(newTile);
            /*
            newTile = new EnvTile(Game, Spritebatch);
            newTile.m_type = EnvTile.EnvironmentType.NEXT;
            newTile.position = new Vector2(1450, 500);
            newTile.m_changed_type = true;
            m_tile_choices.Add(newTile);
            */

            Texture2D tex = Game.Content.Load<Texture2D>("images/selected_tile_glow");
            tile_glow = new Sprite(Spritebatch, tex, Game);
            tile_glow.position = new Vector2(1500, 330);
        }

        public void Reset()
        {
            tile_glow.position = new Vector2(1500, 330);
        }
        public MenuItem ClickCheck(int x, int y)
        {
            MenuItem menuItem = new MenuItem();
            if (false) //change for tool button area.
            {
                /*
                menuItem.m_type = ITEMTYPE.TOOL;
                foreach (EnvTile tile in m_tile_choices)
                {
                    if (tile.Rectangle.Contains(new Point(x, y)))
                    {
                        tile_glow.position = new Vector2(tile.position.X - 10, tile.position.Y - 10);
                        break;
                    }
                    else continue;
                }

                //If we did not click on an enironment.
                menuItem.m_type = ITEMTYPE.NULL;
                return menuItem;

                //*/
            }
            else if (true) //Change for enviroment button area.
            {
                menuItem.m_type = ITEMTYPE.ENVIRONMENT;
                foreach (EnvTile tile in m_tile_choices)
                {
                    if (tile.Rectangle.Contains(new Point(x, y)))
                    {
                        menuItem.m_tileType = tile.m_type;
                        tile_glow.position = new Vector2(tile.position.X, tile.position.Y);
                        return menuItem;
                    }
                    else continue;
                }

                //If we did not click on an enironment.
                menuItem.m_type = ITEMTYPE.NULL;
                return menuItem;
            }
            else
            {
                //if clicked outside "button area" and "enviro area"
                menuItem.m_type = ITEMTYPE.NULL;
                return menuItem;
            }
        }

        public void SetPos(EnvTile.EnvironmentType p_type)
        {
            foreach (EnvTile tile in m_tile_choices)
            {
                if (tile.m_type == p_type)
                {
                    m_tile_selected = tile.m_type;
                    tile_glow.position = new Vector2(tile.position.X, tile.position.Y);
                }
            }
        }

        public override void Initialize()
        {
            base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            foreach (EnvTile tile in m_tile_choices)
            {
                tile.Update(gameTime);
            }
        }

        public override void Draw(GameTime gameTime)
        {
            
            foreach (EnvTile tile in m_tile_choices)
            {
                tile.Draw(gameTime);
            }
            tile_glow.Draw(gameTime);
        }
    }
}
